package com.wframe.project.jnd.wx.controller;

import com.frame.database.PageResult;
import com.frame.util.DateConvert;
import com.wframe.project.jnd.rest.util.*;
import com.wframe.project.jnd.services.ProductService;
import com.wframe.project.jnd.services.PurchaseTempService;
import com.wframe.project.jnd.wx.constant.Constant;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by lwb on 2017/5/10.
 */
@Controller
@RequestMapping("/we")
public class WechatPurchaseTempController extends BaseController {

    @Autowired
    private PurchaseTempService purchaseTempService;

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/purchaseTemplate", method = RequestMethod.POST)
    @ResponseBody
    public String savePurchaseTemplate(String url, String content, String title, ModelMap model) throws ClientException {
        SessionUser sessionUser = WebSession.getWbUser(session);
        if (sessionUser == null) {
            throw new ClientException(CodeEnum.LoginOut);
        }
        JsonReturn json = new JsonReturn();

        model.put("userid", sessionUser.getUserid());
        model.put("photo", url);
        model.put("explanation", content);
        model.put("name", title);
        model.put("createtime", DateConvert.toString(new Date(), "yyyy-MM-dd HH:mm:ss"));

        return json.toString("/we/products/" + purchaseTempService.savePurchaseTemplate(model));
    }

    @RequestMapping(value = "/templateProducts/{id}", method = RequestMethod.GET)
    @ResponseBody
    public String findTemplateProducts(@PathVariable String id,
                                       @RequestParam(value = "pno", defaultValue = "1") Integer pageNo) throws ClientException {
        JsonReturn json = new JsonReturn();
        SessionUser sessionUser = WebSession.getWbUser(session);
        if (sessionUser == null) {
            throw new ClientException(CodeEnum.LoginOut);
        }

        PageResult pr = purchaseTempService.findTemplateProducts(id, Constant.DEFAULT_PAGE_SIZE, pageNo);
        List<Map> productlist = pr.getList();
        for (Map product : productlist) {
            product.put("canbuy", productService.isControlsales(sessionUser.getUserid(), product.get("productid")));
            product.put("amount", Integer.parseInt(String.valueOf(product.get("amount"))));
            product.put("buyamount", Integer.parseInt(String.valueOf(product.get("buyamount"))));
            String photo = product.get("photoview") == null ? "" : product.get("photoview").toString();
            if (StringUtils.isBlank(photo))
                photo = product.get("product_photoview") == null ? "" : product.get("product_photoview").toString();
            product.put("photo", photo);
            product.putAll(productService.findBasicProductAttrValByProductId(product.get("productid")));
        }
        json.putData(pr);
        return json.toString();
    }

}
